package com.bbcare.cp.plat.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.bbcare.comm.Constants;
import com.bbcare.cp.plat.service.IBsStaticParamService;
import com.bbcare.cp.plat.store.ibatis.dictconf.IConfCacheDAO;

@Service
public class BsStaticParamService implements IBsStaticParamService {
	
	Logger logger = Logger.getLogger(BsStaticParamService.class); 
	
	@Autowired
	public IConfCacheDAO confCacheDao;
	
	
	
	
	/**
	 * 根据type、type+code 类型 查询某一类型下的参数值列表 
	 * 先查zuhu类型  如果为空 则查系统配置的类型
	 * @return
	 * @throws Exception 
	 */
	public List<Map<String,Object>> selectBsStaticParamByTenant(Map<String,Object> param) throws Exception{
		/*Map<String,Object> paramTemp = new HashMap<String,Object>();
		paramTemp.put("deptId",param.get("deptId"));
		paramTemp.put("type",param.get("type"));*/
		List<Map<String,Object>> titleMap = confCacheDao.selectBsStaticParamByTenant(param);
		
		if(CollectionUtils.isEmpty(titleMap)){
			param.put("tenantId",Constants.PT_USER_INFO_DEFAULT_TENANT_ID);
			titleMap = confCacheDao.selectBsStaticParamByTenant(param);
		}
		return titleMap;
	}
	
	
	/**
	 * 根据type、type+code 类型 查询某一类型下的参数值列表 
	 * 先查科室下类型  如果为空 则查系统配置的类型
	 * @return
	 * @throws Exception 
	 */
	public List<Map<String,Object>> getStaticParamsByCondition(Map<String,Object> param) throws Exception{
		/*Map<String,Object> paramTemp = new HashMap<String,Object>();
		paramTemp.put("deptId",param.get("deptId"));
		paramTemp.put("type",param.get("type"));*/
		List<Map<String,Object>> titleMap = confCacheDao.selectBsStaticParamByCondition(param);
		
		if(CollectionUtils.isEmpty(titleMap)){
			param.put("deptId",Constants.SYSTEM_CODE_TYPE);
			titleMap = confCacheDao.selectBsStaticParamByCondition(param);
		}
		return titleMap;
	}
	
	
	/**
	 * 将查询到的字典值 按key：value 对应方式 放入map中
	 * @return
	 * @throws Exception 
	 */
	public Map<String,String> getStaticParamsForMap(Map<String,Object> param) throws Exception{
		/*Map<String,Object> paramTemp = new HashMap<String,Object>();
		paramTemp.put("deptId",param.get("deptId"));
		paramTemp.put("type",param.get("type"));*/
		
		//TODO 从缓存获取
		List<Map<String,Object>> titleList = confCacheDao.selectBsStaticParamByCondition(param);
		
		if(CollectionUtils.isEmpty(titleList)){
			param.put("deptId","0"); //0 代表系统级配置
			titleList = confCacheDao.selectBsStaticParamByCondition(param);
		}
		
		Map<String,String> staticMap = new HashMap<String,String>();
		for(int i = 0;i<titleList.size();i++){
			String key = (String)titleList.get(i).get("paramCode");
			String value = (String)titleList.get(i).get("paramName");
			staticMap.put(key, value);
		}
		return staticMap;
	}
	
}
